package utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zip {
	static final String ZIP_EXTENSION = ".zip";

	public static void zipFiles(String path, String name, List<File> files) {
		try {

			BufferedInputStream origin = null;
			FileOutputStream dest = new FileOutputStream(path + File.separatorChar 
					+ name + ZIP_EXTENSION);
			CheckedOutputStream checksum = new CheckedOutputStream(dest,
					new Adler32());
			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
					checksum));
			byte data[] = null;			
			
			for (int i = 0; i < files.size(); i++) {
				System.out.println("Adding: " + files.get(i));

				FileInputStream fi = new FileInputStream(files.get(i));
				
				int lengthFile = 1024;

				data = new byte[lengthFile];

				origin = new BufferedInputStream(fi, lengthFile);
				ZipEntry entry = new ZipEntry("Formulario Empresa_" + i);
				out.putNextEntry(entry);
				
				int count;
				while ((count = origin.read(data, 0, lengthFile)) != -1) {
					out.write(data, 0, count);
				}
				origin.close();
			}
			out.close();
			System.out.println("checksum:" + checksum.getChecksum().getValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}